// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0

//! This pass computes the layout constraint

use lyon_path::geom::euclid::approxeq::ApproxEq;

use crate::diagnostics::{BuildDiagnostics, DiagnosticLevel, Spanned};
use crate::expression_tree::*;
use crate::langtype::ElementType;
use crate::langtype::Type;
use crate::layout::*;
use crate::object_tree::*;
use crate::typeloader::TypeLoader;
use crate::typeregister::{TypeRegister, layout_info_type, organized_layout_type};
use smol_str::{SmolStr, format_smolstr};
use std::cell::RefCell;
use std::collections::HashSet;
use std::rc::Rc;

pub fn lower_layouts(
    component: &Rc<Component>,
    type_loader: &mut TypeLoader,
    style_metrics: &Rc<Component>,
    diag: &mut BuildDiagnostics,
) {
    // lower the preferred-{width, height}: 100%;
    recurse_elem_including_sub_components(component, &(), &mut |elem, _| {
        if check_preferred_size_100(elem, "preferred-width", diag) {
            elem.borrow_mut().default_fill_parent.0 = true;
        }
        if check_preferred_size_100(elem, "preferred-height", diag) {
            elem.borrow_mut().default_fill_parent.1 = true;
        }
        let base = elem.borrow().sub_component().cloned();
        if let Some(base) = base {
            let base = base.root_element.borrow();
            let mut elem_mut = elem.borrow_mut();
            elem_mut.default_fill_parent.0 |= base.default_fill_parent.0;
            elem_mut.default_fill_parent.1 |= base.default_fill_parent.1;
        }
    });

    *component.root_constraints.borrow_mut() =
        LayoutConstraints::new(&component.root_element, diag, DiagnosticLevel::Error);

    recurse_elem_including_sub_components(
        component,
        &Option::default(),
        &mut |elem, parent_layout_type| {
            let component = elem.borrow().enclosing_component.upgrade().unwrap();

            lower_element_layout(
                &component,
                elem,
                &type_loader.global_type_registry.borrow(),
                style_metrics,
                parent_layout_type,
                diag,
            )
        },
    );
}

fn check_preferred_size_100(elem: &ElementRc, prop: &str, diag: &mut BuildDiagnostics) -> bool {
    let ret = if let Some(p) = elem.borrow().bindings.get(prop) {
        if p.borrow().expression.ty() == Type::Percent {
            if !matches!(p.borrow().expression.ignore_debug_hooks(), Expression::NumberLiteral(val, _) if *val == 100.)
            {
                diag.push_error(
                    format!("{prop} must either be a length, or the literal '100%'"),
                    &*p.borrow(),
                );
            }
            true
        } else {
            false
        }
    } else {
        false
    };
    if ret {
        elem.borrow_mut().bindings.remove(prop).unwrap();
        return true;
    }
    false
}

/// If the element is a layout, lower it to a Rectangle, and set the geometry property of the element inside it.
/// Returns the name of the layout type if the element was a layout and has been lowered
fn lower_element_layout(
    component: &Rc<Component>,
    elem: &ElementRc,
    type_register: &TypeRegister,
    style_metrics: &Rc<Component>,
    parent_layout_type: &Option<SmolStr>,
    diag: &mut BuildDiagnostics,
) -> Option<SmolStr> {
    let layout_type = if let ElementType::Builtin(base_type) = &elem.borrow().base_type {
        Some(base_type.name.clone())
    } else {
        None
    };

    check_no_layout_properties(elem, &layout_type, parent_layout_type, diag);

    layout_type.as_ref()?;

    match layout_type.as_ref().unwrap().as_str() {
        "Row" => {
            // We shouldn't lower layout if we have a Row in there. Unless the Row is the root of a repeated item,
            // in which case another error has been reported
            assert!(
                diag.has_errors()
                    && Rc::ptr_eq(&component.root_element, elem)
                    && component
                        .parent_element
                        .upgrade()
                        .is_some_and(|e| e.borrow().repeated.is_some()),
                "Error should have been caught at element lookup time"
            );
            return None;
        }
        "GridLayout" => lower_grid_layout(component, elem, diag, type_register),
        "HorizontalLayout" => lower_box_layout(elem, diag, Orientation::Horizontal),
        "VerticalLayout" => lower_box_layout(elem, diag, Orientation::Vertical),
        "Dialog" => {
            lower_dialog_layout(elem, style_metrics, diag);
            // return now, the Dialog stays in the tree as a Dialog
            return layout_type;
        }
        _ => return None,
    };

    let mut elem = elem.borrow_mut();
    let elem = &mut *elem;
    let prev_base = std::mem::replace(&mut elem.base_type, type_register.empty_type());
    elem.default_fill_parent = (true, true);
    // Create fake properties for the layout properties
    // like alignment, spacing, spacing-horizontal, spacing-vertical
    for (p, ty) in prev_base.property_list() {
        if !elem.base_type.lookup_property(&p).is_valid()
            && !elem.property_declarations.contains_key(&p)
        {
            elem.property_declarations.insert(p, ty.into());
        }
    }

    layout_type
}

// to detect mixing auto and non-literal expressions in row/col values
#[derive(Debug, PartialEq, Eq)]
enum RowColExpressionType {
    Auto, // not specified
    Literal,
    RuntimeExpression,
}
impl RowColExpressionType {
    fn from_option_expr(
        expr: &Option<Expression>,
        is_number_literal: bool,
    ) -> RowColExpressionType {
        match expr {
            None => RowColExpressionType::Auto,
            Some(_) if is_number_literal => RowColExpressionType::Literal,
            Some(_) => RowColExpressionType::RuntimeExpression,
        }
    }
}

fn lower_grid_layout(
    component: &Rc<Component>,
    grid_layout_element: &ElementRc,
    diag: &mut BuildDiagnostics,
    type_register: &TypeRegister,
) {
    let mut grid = GridLayout {
        elems: Default::default(),
        geometry: LayoutGeometry::new(grid_layout_element),
        dialog_button_roles: None,
        uses_auto: false,
    };

    let layout_organized_data_prop = create_new_prop(
        grid_layout_element,
        SmolStr::new_static("layout-organized-data"),
        organized_layout_type(),
    );
    let layout_cache_prop_h = create_new_prop(
        grid_layout_element,
        SmolStr::new_static("layout-cache-h"),
        Type::LayoutCache,
    );
    let layout_cache_prop_v = create_new_prop(
        grid_layout_element,
        SmolStr::new_static("layout-cache-v"),
        Type::LayoutCache,
    );
    let layout_info_prop_h = create_new_prop(
        grid_layout_element,
        SmolStr::new_static("layoutinfo-h"),
        layout_info_type().into(),
    );
    let layout_info_prop_v = create_new_prop(
        grid_layout_element,
        SmolStr::new_static("layoutinfo-v"),
        layout_info_type().into(),
    );

    let layout_children = std::mem::take(&mut grid_layout_element.borrow_mut().children);
    let mut collected_children = Vec::new();
    let mut new_row = false; // true until the first child of a Row, or the first item after an empty Row
    let mut numbering_type: Option<RowColExpressionType> = None;
    for layout_child in layout_children {
        let is_row = if let ElementType::Builtin(be) = &layout_child.borrow().base_type {
            be.name == "Row"
        } else {
            false
        };
        if is_row {
            new_row = true;
            let row_children = std::mem::take(&mut layout_child.borrow_mut().children);
            for x in row_children {
                if let Some(binding) = x.borrow_mut().bindings.get("row") {
                    diag.push_error(
                        "The 'row' property cannot be used for elements inside a Row".to_string(),
                        &*binding.borrow(),
                    );
                }
                grid.add_element(
                    &x,
                    new_row,
                    &layout_cache_prop_h,
                    &layout_cache_prop_v,
                    &layout_organized_data_prop,
                    &mut numbering_type,
                    diag,
                );
                collected_children.push(x);
                new_row = false;
            }
            new_row = true; // the end of a Row means the next item is the first of a new row
            if layout_child.borrow().has_popup_child {
                // We need to keep that element otherwise the popup will malfunction
                layout_child.borrow_mut().base_type = type_register.empty_type();
                collected_children.push(layout_child);
            } else {
                component.optimized_elements.borrow_mut().push(layout_child);
            }
        } else {
            grid.add_element(
                &layout_child,
                new_row,
                &layout_cache_prop_h,
                &layout_cache_prop_v,
                &layout_organized_data_prop,
                &mut numbering_type,
                diag,
            );
            collected_children.push(layout_child);
            new_row = false;
        }
    }
    grid_layout_element.borrow_mut().children = collected_children;
    grid.uses_auto = numbering_type == Some(RowColExpressionType::Auto);
    let span = grid_layout_element.borrow().to_source_location();

    layout_organized_data_prop.element().borrow_mut().bindings.insert(
        layout_organized_data_prop.name().clone(),
        BindingExpression::new_with_span(
            Expression::OrganizeGridLayout(grid.clone()),
            span.clone(),
        )
        .into(),
    );
    layout_cache_prop_h.element().borrow_mut().bindings.insert(
        layout_cache_prop_h.name().clone(),
        BindingExpression::new_with_span(
            Expression::SolveGridLayout {
                layout_organized_data_prop: layout_organized_data_prop.clone(),
                layout: grid.clone(),
                orientation: Orientation::Horizontal,
            },
            span.clone(),
        )
        .into(),
    );
    layout_cache_prop_v.element().borrow_mut().bindings.insert(
        layout_cache_prop_v.name().clone(),
        BindingExpression::new_with_span(
            Expression::SolveGridLayout {
                layout_organized_data_prop: layout_organized_data_prop.clone(),
                layout: grid.clone(),
                orientation: Orientation::Vertical,
            },
            span.clone(),
        )
        .into(),
    );
    layout_info_prop_h.element().borrow_mut().bindings.insert(
        layout_info_prop_h.name().clone(),
        BindingExpression::new_with_span(
            Expression::ComputeGridLayoutInfo {
                layout_organized_data_prop: layout_organized_data_prop.clone(),
                layout: grid.clone(),
                orientation: Orientation::Horizontal,
            },
            span.clone(),
        )
        .into(),
    );
    layout_info_prop_v.element().borrow_mut().bindings.insert(
        layout_info_prop_v.name().clone(),
        BindingExpression::new_with_span(
            Expression::ComputeGridLayoutInfo {
                layout_organized_data_prop: layout_organized_data_prop.clone(),
                layout: grid.clone(),
                orientation: Orientation::Vertical,
            },
            span,
        )
        .into(),
    );
    grid_layout_element.borrow_mut().layout_info_prop =
        Some((layout_info_prop_h, layout_info_prop_v));
    for d in grid_layout_element.borrow_mut().debug.iter_mut() {
        d.layout = Some(Layout::GridLayout(grid.clone()));
    }
}

impl GridLayout {
    fn add_element(
        &mut self,
        item_element: &ElementRc,
        new_row: bool,
        layout_cache_prop_h: &NamedReference,
        layout_cache_prop_v: &NamedReference,
        organized_data_prop: &NamedReference,
        numbering_type: &mut Option<RowColExpressionType>,
        diag: &mut BuildDiagnostics,
    ) {
        // Some compile-time checks
        {
            let mut check_expr = |name: &str| {
                let mut is_number_literal = false;
                let expr = item_element.borrow_mut().bindings.get(name).map(|e| {
                    let expr = &e.borrow().expression;
                    is_number_literal =
                        check_number_literal_is_positive_integer(expr, name, &*e.borrow(), diag);
                    expr.clone()
                });
                (expr, is_number_literal)
            };

            let (row_expr, row_is_number_literal) = check_expr("row");
            let (col_expr, col_is_number_literal) = check_expr("col");
            check_expr("rowspan");
            check_expr("colspan");

            let mut check_numbering_consistency =
                |expr_type: RowColExpressionType, prop_name: &str| {
                    if !matches!(expr_type, RowColExpressionType::Literal) {
                        if let Some(current_numbering_type) = numbering_type {
                            if *current_numbering_type != expr_type {
                                let element_ref = item_element.borrow();
                                let span: &dyn Spanned =
                                    if let Some(binding) = element_ref.bindings.get(prop_name) {
                                        &*binding.borrow()
                                    } else {
                                        &*element_ref
                                    };
                                diag.push_error(
                                    format!("Cannot mix auto-numbering and runtime expressions for the '{prop_name}' property"),
                                    span,
                                );
                            }
                        } else {
                            // Store the first auto or runtime expression case we see
                            *numbering_type = Some(expr_type);
                        }
                    }
                };

            let row_expr_type =
                RowColExpressionType::from_option_expr(&row_expr, row_is_number_literal);
            check_numbering_consistency(row_expr_type, "row");

            let col_expr_type =
                RowColExpressionType::from_option_expr(&col_expr, col_is_number_literal);
            check_numbering_consistency(col_expr_type, "col");
        }

        let propref_or_default = |name: &'static str| -> Option<RowColExpr> {
            crate::layout::binding_reference(item_element, name).map(RowColExpr::Named)
        };

        // MAX means "auto", see to_layout_data()
        let row_expr = propref_or_default("row");
        let col_expr = propref_or_default("col");
        let rowspan_expr = propref_or_default("rowspan");
        let colspan_expr = propref_or_default("colspan");

        self.add_element_with_coord_as_expr(
            item_element,
            new_row,
            (&row_expr, &col_expr),
            (&rowspan_expr, &colspan_expr),
            layout_cache_prop_h,
            layout_cache_prop_v,
            organized_data_prop,
            diag,
        );
    }

    fn add_element_with_coord(
        &mut self,
        item_element: &ElementRc,
        (row, col): (u16, u16),
        (rowspan, colspan): (u16, u16),
        layout_cache_prop_h: &NamedReference,
        layout_cache_prop_v: &NamedReference,
        organized_data_prop: &NamedReference,
        diag: &mut BuildDiagnostics,
    ) {
        self.add_element_with_coord_as_expr(
            item_element,
            false, // new_row
            (&Some(RowColExpr::Literal(row)), &Some(RowColExpr::Literal(col))),
            (&Some(RowColExpr::Literal(rowspan)), &Some(RowColExpr::Literal(colspan))),
            layout_cache_prop_h,
            layout_cache_prop_v,
            organized_data_prop,
            diag,
        )
    }

    fn add_element_with_coord_as_expr(
        &mut self,
        item_element: &ElementRc,
        new_row: bool,
        (row_expr, col_expr): (&Option<RowColExpr>, &Option<RowColExpr>),
        (rowspan_expr, colspan_expr): (&Option<RowColExpr>, &Option<RowColExpr>),
        layout_cache_prop_h: &NamedReference,
        layout_cache_prop_v: &NamedReference,
        organized_data_prop: &NamedReference,
        diag: &mut BuildDiagnostics,
    ) {
        let index = self.elems.len();
        let result = create_layout_item(item_element, diag);
        if let Some(ref layout_item) = result {
            if layout_item.repeater_index.is_some() {
                diag.push_error(
                    "'if' or 'for' expressions are not currently supported in grid layouts"
                        .to_string(),
                    &*item_element.borrow(),
                );
                return;
            }

            let e = &layout_item.elem;
            set_prop_from_cache(e, "x", layout_cache_prop_h, index * 2, &None, diag);
            if !layout_item.item.constraints.fixed_width {
                set_prop_from_cache(e, "width", layout_cache_prop_h, index * 2 + 1, &None, diag);
            }
            set_prop_from_cache(e, "y", layout_cache_prop_v, index * 2, &None, diag);
            if !layout_item.item.constraints.fixed_height {
                set_prop_from_cache(e, "height", layout_cache_prop_v, index * 2 + 1, &None, diag);
            }

            let org_index = index * 4;
            if col_expr.is_none() {
                set_prop_from_cache(e, "col", organized_data_prop, org_index, &None, diag);
            }
            if row_expr.is_none() {
                set_prop_from_cache(e, "row", organized_data_prop, org_index + 2, &None, diag);
            }

            let expr_or_default = |expr: &Option<RowColExpr>, default: u16| -> RowColExpr {
                match expr {
                    Some(RowColExpr::Literal(v)) => RowColExpr::Literal(*v),
                    Some(RowColExpr::Named(nr)) => RowColExpr::Named(nr.clone()),
                    None => RowColExpr::Literal(default),
                }
            };

            self.elems.push(GridLayoutElement {
                new_row,
                col_expr: expr_or_default(col_expr, u16::MAX), // MAX means "auto"
                row_expr: expr_or_default(row_expr, u16::MAX),
                colspan_expr: expr_or_default(colspan_expr, 1),
                rowspan_expr: expr_or_default(rowspan_expr, 1),
                item: layout_item.item.clone(),
            });
        }
    }
}

fn lower_box_layout(
    layout_element: &ElementRc,
    diag: &mut BuildDiagnostics,
    orientation: Orientation,
) {
    let mut layout = BoxLayout {
        orientation,
        elems: Default::default(),
        geometry: LayoutGeometry::new(layout_element),
    };

    let layout_cache_prop =
        create_new_prop(layout_element, SmolStr::new_static("layout-cache"), Type::LayoutCache);
    let layout_info_prop_v = create_new_prop(
        layout_element,
        SmolStr::new_static("layoutinfo-v"),
        layout_info_type().into(),
    );
    let layout_info_prop_h = create_new_prop(
        layout_element,
        SmolStr::new_static("layoutinfo-h"),
        layout_info_type().into(),
    );

    let layout_children = std::mem::take(&mut layout_element.borrow_mut().children);

    let (begin_padding, end_padding) = match orientation {
        Orientation::Horizontal => (&layout.geometry.padding.top, &layout.geometry.padding.bottom),
        Orientation::Vertical => (&layout.geometry.padding.left, &layout.geometry.padding.right),
    };
    let (pos, size, pad, ortho) = match orientation {
        Orientation::Horizontal => ("x", "width", "y", "height"),
        Orientation::Vertical => ("y", "height", "x", "width"),
    };
    let pad_expr = begin_padding.clone().map(Expression::PropertyReference);
    let mut size_expr = Expression::PropertyReference(NamedReference::new(
        layout_element,
        SmolStr::new_static(ortho),
    ));
    if let Some(p) = begin_padding {
        size_expr = Expression::BinaryExpression {
            lhs: Box::new(std::mem::take(&mut size_expr)),
            rhs: Box::new(Expression::PropertyReference(p.clone())),
            op: '-',
        }
    }
    if let Some(p) = end_padding {
        size_expr = Expression::BinaryExpression {
            lhs: Box::new(std::mem::take(&mut size_expr)),
            rhs: Box::new(Expression::PropertyReference(p.clone())),
            op: '-',
        }
    }

    for layout_child in &layout_children {
        if let Some(item) = create_layout_item(layout_child, diag) {
            let index = layout.elems.len() * 2;
            let rep_idx = &item.repeater_index;
            let (fixed_size, fixed_ortho) = match orientation {
                Orientation::Horizontal => {
                    (item.item.constraints.fixed_width, item.item.constraints.fixed_height)
                }
                Orientation::Vertical => {
                    (item.item.constraints.fixed_height, item.item.constraints.fixed_width)
                }
            };
            let actual_elem = &item.elem;
            set_prop_from_cache(actual_elem, pos, &layout_cache_prop, index, rep_idx, diag);
            if !fixed_size {
                set_prop_from_cache(
                    actual_elem,
                    size,
                    &layout_cache_prop,
                    index + 1,
                    rep_idx,
                    diag,
                );
            }
            if let Some(pad_expr) = pad_expr.clone() {
                actual_elem.borrow_mut().bindings.insert(pad.into(), RefCell::new(pad_expr.into()));
            }
            if !fixed_ortho {
                actual_elem
                    .borrow_mut()
                    .bindings
                    .insert(ortho.into(), RefCell::new(size_expr.clone().into()));
            }
            layout.elems.push(item.item);
        }
    }
    layout_element.borrow_mut().children = layout_children;
    let span = layout_element.borrow().to_source_location();
    layout_cache_prop.element().borrow_mut().bindings.insert(
        layout_cache_prop.name().clone(),
        BindingExpression::new_with_span(
            Expression::SolveLayout(Layout::BoxLayout(layout.clone()), orientation),
            span.clone(),
        )
        .into(),
    );
    layout_info_prop_h.element().borrow_mut().bindings.insert(
        layout_info_prop_h.name().clone(),
        BindingExpression::new_with_span(
            Expression::ComputeLayoutInfo(
                Layout::BoxLayout(layout.clone()),
                Orientation::Horizontal,
            ),
            span.clone(),
        )
        .into(),
    );
    layout_info_prop_v.element().borrow_mut().bindings.insert(
        layout_info_prop_v.name().clone(),
        BindingExpression::new_with_span(
            Expression::ComputeLayoutInfo(Layout::BoxLayout(layout.clone()), Orientation::Vertical),
            span,
        )
        .into(),
    );
    layout_element.borrow_mut().layout_info_prop = Some((layout_info_prop_h, layout_info_prop_v));
    for d in layout_element.borrow_mut().debug.iter_mut() {
        d.layout = Some(Layout::BoxLayout(layout.clone()));
    }
}

fn lower_dialog_layout(
    dialog_element: &ElementRc,
    style_metrics: &Rc<Component>,
    diag: &mut BuildDiagnostics,
) {
    let mut grid = GridLayout {
        elems: Default::default(),
        geometry: LayoutGeometry::new(dialog_element),
        dialog_button_roles: None,
        uses_auto: true,
    };
    let metrics = &style_metrics.root_element;
    grid.geometry
        .padding
        .bottom
        .get_or_insert(NamedReference::new(metrics, SmolStr::new_static("layout-padding")));
    grid.geometry
        .padding
        .top
        .get_or_insert(NamedReference::new(metrics, SmolStr::new_static("layout-padding")));
    grid.geometry
        .padding
        .left
        .get_or_insert(NamedReference::new(metrics, SmolStr::new_static("layout-padding")));
    grid.geometry
        .padding
        .right
        .get_or_insert(NamedReference::new(metrics, SmolStr::new_static("layout-padding")));
    grid.geometry
        .spacing
        .horizontal
        .get_or_insert(NamedReference::new(metrics, SmolStr::new_static("layout-spacing")));
    grid.geometry
        .spacing
        .vertical
        .get_or_insert(NamedReference::new(metrics, SmolStr::new_static("layout-spacing")));

    let layout_organized_data_prop = create_new_prop(
        dialog_element,
        SmolStr::new_static("layout-organized-data"),
        organized_layout_type(),
    );
    let layout_cache_prop_h =
        create_new_prop(dialog_element, SmolStr::new_static("layout-cache-h"), Type::LayoutCache);
    let layout_cache_prop_v =
        create_new_prop(dialog_element, SmolStr::new_static("layout-cache-v"), Type::LayoutCache);
    let layout_info_prop_h = create_new_prop(
        dialog_element,
        SmolStr::new_static("layoutinfo-h"),
        layout_info_type().into(),
    );
    let layout_info_prop_v = create_new_prop(
        dialog_element,
        SmolStr::new_static("layoutinfo-v"),
        layout_info_type().into(),
    );

    let mut main_widget = None;
    let mut button_roles = Vec::new();
    let mut seen_buttons = HashSet::new();
    let layout_children = std::mem::take(&mut dialog_element.borrow_mut().children);
    for layout_child in &layout_children {
        let dialog_button_role_binding =
            layout_child.borrow_mut().bindings.remove("dialog-button-role");
        let is_button = if let Some(role_binding) = dialog_button_role_binding {
            let role_binding = role_binding.into_inner();
            if let Expression::EnumerationValue(val) =
                super::ignore_debug_hooks(&role_binding.expression)
            {
                let en = &val.enumeration;
                debug_assert_eq!(en.name, "DialogButtonRole");
                button_roles.push(en.values[val.value].clone());
                if val.value == 0 {
                    diag.push_error(
                        "The `dialog-button-role` cannot be set explicitly to none".into(),
                        &role_binding,
                    );
                }
            } else {
                diag.push_error(
                    "The `dialog-button-role` property must be known at compile-time".into(),
                    &role_binding,
                );
            }
            true
        } else if matches!(&layout_child.borrow().lookup_property("kind").property_type, Type::Enumeration(e) if e.name == "StandardButtonKind")
        {
            // layout_child is a StandardButton
            match layout_child.borrow().bindings.get("kind") {
                None => diag.push_error(
                    "The `kind` property of the StandardButton in a Dialog must be set".into(),
                    &*layout_child.borrow(),
                ),
                Some(binding) => {
                    let binding = &*binding.borrow();
                    if let Expression::EnumerationValue(val) =
                        super::ignore_debug_hooks(&binding.expression)
                    {
                        let en = &val.enumeration;
                        debug_assert_eq!(en.name, "StandardButtonKind");
                        let kind = &en.values[val.value];
                        let role = match kind.as_str() {
                            "ok" => "accept",
                            "cancel" => "reject",
                            "apply" => "apply",
                            "close" => "reject",
                            "reset" => "reset",
                            "help" => "help",
                            "yes" => "accept",
                            "no" => "reject",
                            "abort" => "reject",
                            "retry" => "accept",
                            "ignore" => "accept",
                            _ => unreachable!(),
                        };
                        button_roles.push(role.into());
                        if !seen_buttons.insert(val.value) {
                            diag.push_error("Duplicated `kind`: There are two StandardButton in this Dialog with the same kind".into(), binding);
                        } else if Rc::ptr_eq(
                            dialog_element,
                            &dialog_element
                                .borrow()
                                .enclosing_component
                                .upgrade()
                                .unwrap()
                                .root_element,
                        ) {
                            let clicked_ty =
                                layout_child.borrow().lookup_property("clicked").property_type;
                            if matches!(&clicked_ty, Type::Callback { .. })
                                && layout_child.borrow().bindings.get("clicked").is_none_or(|c| {
                                    matches!(c.borrow().expression, Expression::Invalid)
                                })
                            {
                                dialog_element
                                    .borrow_mut()
                                    .property_declarations
                                    .entry(format_smolstr!("{}-clicked", kind))
                                    .or_insert_with(|| PropertyDeclaration {
                                        property_type: clicked_ty,
                                        node: None,
                                        expose_in_public_api: true,
                                        is_alias: Some(NamedReference::new(
                                            layout_child,
                                            SmolStr::new_static("clicked"),
                                        )),
                                        visibility: PropertyVisibility::InOut,
                                        pure: None,
                                    });
                            }
                        }
                    } else {
                        diag.push_error(
                            "The `kind` property of the StandardButton in a Dialog must be known at compile-time"
                                .into(),
                            binding,
                        );
                    }
                }
            }
            true
        } else {
            false
        };

        if is_button {
            grid.add_element_with_coord(
                layout_child,
                (1, button_roles.len() as u16),
                (1, 1),
                &layout_cache_prop_h,
                &layout_cache_prop_v,
                &layout_organized_data_prop,
                diag,
            );
        } else if main_widget.is_some() {
            diag.push_error(
                "A Dialog can have only one child element that is not a StandardButton".into(),
                &*layout_child.borrow(),
            );
        } else {
            main_widget = Some(layout_child.clone())
        }
    }
    dialog_element.borrow_mut().children = layout_children;

    if let Some(main_widget) = main_widget {
        grid.add_element_with_coord(
            &main_widget,
            (0, 0),
            (1, button_roles.len() as u16 + 1),
            &layout_cache_prop_h,
            &layout_cache_prop_v,
            &layout_organized_data_prop,
            diag,
        );
    } else {
        diag.push_error(
            "A Dialog must have a single child element that is not StandardButton".into(),
            &*dialog_element.borrow(),
        );
    }
    grid.dialog_button_roles = Some(button_roles);

    let span = dialog_element.borrow().to_source_location();
    layout_organized_data_prop.element().borrow_mut().bindings.insert(
        layout_organized_data_prop.name().clone(),
        BindingExpression::new_with_span(
            Expression::OrganizeGridLayout(grid.clone()),
            span.clone(),
        )
        .into(),
    );
    layout_cache_prop_h.element().borrow_mut().bindings.insert(
        layout_cache_prop_h.name().clone(),
        BindingExpression::new_with_span(
            Expression::SolveGridLayout {
                layout_organized_data_prop: layout_organized_data_prop.clone(),
                layout: grid.clone(),
                orientation: Orientation::Horizontal,
            },
            span.clone(),
        )
        .into(),
    );
    layout_cache_prop_v.element().borrow_mut().bindings.insert(
        layout_cache_prop_v.name().clone(),
        BindingExpression::new_with_span(
            Expression::SolveGridLayout {
                layout_organized_data_prop: layout_organized_data_prop.clone(),
                layout: grid.clone(),
                orientation: Orientation::Vertical,
            },
            span.clone(),
        )
        .into(),
    );
    layout_info_prop_h.element().borrow_mut().bindings.insert(
        layout_info_prop_h.name().clone(),
        BindingExpression::new_with_span(
            Expression::ComputeGridLayoutInfo {
                layout_organized_data_prop: layout_organized_data_prop.clone(),
                layout: grid.clone(),
                orientation: Orientation::Horizontal,
            },
            span.clone(),
        )
        .into(),
    );
    layout_info_prop_v.element().borrow_mut().bindings.insert(
        layout_info_prop_v.name().clone(),
        BindingExpression::new_with_span(
            Expression::ComputeGridLayoutInfo {
                layout_organized_data_prop: layout_organized_data_prop.clone(),
                layout: grid.clone(),
                orientation: Orientation::Vertical,
            },
            span,
        )
        .into(),
    );
    dialog_element.borrow_mut().layout_info_prop = Some((layout_info_prop_h, layout_info_prop_v));
    for d in dialog_element.borrow_mut().debug.iter_mut() {
        d.layout = Some(Layout::GridLayout(grid.clone()));
    }
}

struct CreateLayoutItemResult {
    item: LayoutItem,
    elem: ElementRc,
    repeater_index: Option<Expression>,
}

/// Create a LayoutItem for the given `item_element`  returns None is the layout is empty
fn create_layout_item(
    item_element: &ElementRc,
    diag: &mut BuildDiagnostics,
) -> Option<CreateLayoutItemResult> {
    let fix_explicit_percent = |prop: &str, item: &ElementRc| {
        if !item.borrow().bindings.get(prop).is_some_and(|b| b.borrow().ty() == Type::Percent) {
            return;
        }
        let min_name = format_smolstr!("min-{}", prop);
        let max_name = format_smolstr!("max-{}", prop);
        let mut min_ref = BindingExpression::from(Expression::PropertyReference(
            NamedReference::new(item, min_name.clone()),
        ));
        let mut item = item.borrow_mut();
        let b = item.bindings.remove(prop).unwrap().into_inner();
        min_ref.span = b.span.clone();
        min_ref.priority = b.priority;
        item.bindings.insert(max_name.clone(), min_ref.into());
        item.bindings.insert(min_name.clone(), b.into());
        item.property_declarations.insert(
            min_name,
            PropertyDeclaration { property_type: Type::Percent, ..PropertyDeclaration::default() },
        );
        item.property_declarations.insert(
            max_name,
            PropertyDeclaration { property_type: Type::Percent, ..PropertyDeclaration::default() },
        );
    };
    fix_explicit_percent("width", item_element);
    fix_explicit_percent("height", item_element);

    item_element.borrow_mut().child_of_layout = true;
    let (repeater_index, actual_elem) = if let Some(r) = &item_element.borrow().repeated {
        let rep_comp = item_element.borrow().base_type.as_component().clone();
        fix_explicit_percent("width", &rep_comp.root_element);
        fix_explicit_percent("height", &rep_comp.root_element);

        *rep_comp.root_constraints.borrow_mut() =
            LayoutConstraints::new(&rep_comp.root_element, diag, DiagnosticLevel::Error);
        rep_comp.root_element.borrow_mut().child_of_layout = true;
        (
            Some(if r.is_conditional_element {
                Expression::NumberLiteral(0., Unit::None)
            } else {
                Expression::RepeaterIndexReference { element: Rc::downgrade(item_element) }
            }),
            rep_comp.root_element.clone(),
        )
    } else {
        (None, item_element.clone())
    };

    let constraints = LayoutConstraints::new(&actual_elem, diag, DiagnosticLevel::Error);
    Some(CreateLayoutItemResult {
        item: LayoutItem { element: item_element.clone(), constraints },
        elem: actual_elem,
        repeater_index,
    })
}

fn set_prop_from_cache(
    elem: &ElementRc,
    prop: &str,
    layout_cache_prop: &NamedReference,
    index: usize,
    repeater_index: &Option<Expression>,
    diag: &mut BuildDiagnostics,
) {
    let old = elem.borrow_mut().bindings.insert(
        prop.into(),
        BindingExpression::new_with_span(
            Expression::LayoutCacheAccess {
                layout_cache_prop: layout_cache_prop.clone(),
                index,
                repeater_index: repeater_index.as_ref().map(|x| Box::new(x.clone())),
            },
            layout_cache_prop.element().borrow().to_source_location(),
        )
        .into(),
    );
    if let Some(old) = old.map(RefCell::into_inner) {
        diag.push_error(
            format!("The property '{prop}' cannot be set for elements placed in this layout, because the layout is already setting it"),
            &old,
        );
    }
}

// If it's a number literal, it must be a positive integer
// But also allow any other kind of expression
// Returns true for literals, false for other kinds of expressions
fn check_number_literal_is_positive_integer(
    expression: &Expression,
    name: &str,
    span: &dyn crate::diagnostics::Spanned,
    diag: &mut BuildDiagnostics,
) -> bool {
    match super::ignore_debug_hooks(expression) {
        Expression::NumberLiteral(v, Unit::None) => {
            if *v > u16::MAX as f64 || !v.trunc().approx_eq(v) {
                diag.push_error(format!("'{name}' must be a positive integer"), span);
            }
            true
        }
        Expression::UnaryOp { op: '-', sub } => {
            if let Expression::NumberLiteral(_, Unit::None) = super::ignore_debug_hooks(sub) {
                diag.push_error(format!("'{name}' must be a positive integer"), span);
            }
            true
        }
        Expression::Cast { from, .. } => {
            check_number_literal_is_positive_integer(from, name, span, diag)
        }
        _ => false,
    }
}

fn recognized_layout_types() -> &'static [&'static str] {
    &["Row", "GridLayout", "HorizontalLayout", "VerticalLayout", "Dialog"]
}

/// Checks that there are no grid-layout specific properties used wrongly
fn check_no_layout_properties(
    item: &ElementRc,
    layout_type: &Option<SmolStr>,
    parent_layout_type: &Option<SmolStr>,
    diag: &mut BuildDiagnostics,
) {
    let elem = item.borrow();
    for (prop, expr) in elem.bindings.iter() {
        if parent_layout_type.as_deref() != Some("GridLayout")
            && matches!(prop.as_ref(), "col" | "row" | "colspan" | "rowspan")
        {
            diag.push_error(format!("{prop} used outside of a GridLayout's cell"), &*expr.borrow());
        }
        if parent_layout_type.as_deref() != Some("Dialog")
            && matches!(prop.as_ref(), "dialog-button-role")
        {
            diag.push_error(
                format!("{prop} used outside of a Dialog's direct child"),
                &*expr.borrow(),
            );
        }
        if (layout_type.is_none()
            || !recognized_layout_types().contains(&layout_type.as_ref().unwrap().as_str()))
            && matches!(
                prop.as_ref(),
                "padding" | "padding-left" | "padding-right" | "padding-top" | "padding-bottom"
            )
            && !check_inherits_layout(item)
        {
            diag.push_warning(
                format!("{prop} only has effect on layout elements"),
                &*expr.borrow(),
            );
        }
    }

    /// Check if the element inherits from a layout that was lowered
    fn check_inherits_layout(item: &ElementRc) -> bool {
        if let ElementType::Component(c) = &item.borrow().base_type {
            c.root_element.borrow().debug.iter().any(|d| d.layout.is_some())
                || check_inherits_layout(&c.root_element)
        } else {
            false
        }
    }
}

/// For fixed layout, we need to dissociate the width and the height property of the WindowItem from width and height property
/// in slint such that the width and height property are actually constants.
///
/// The Slint runtime will change the width and height property of the native WindowItem to match those of the actual
/// window, but we don't want that to happen if we have a fixed layout.
pub fn check_window_layout(component: &Rc<Component>) {
    if component.root_constraints.borrow().fixed_height {
        adjust_window_layout(component, "height");
    }
    if component.root_constraints.borrow().fixed_width {
        adjust_window_layout(component, "width");
    }
}

fn adjust_window_layout(component: &Rc<Component>, prop: &'static str) {
    let new_prop = crate::layout::create_new_prop(
        &component.root_element,
        format_smolstr!("fixed-{prop}"),
        Type::LogicalLength,
    );
    {
        let mut root = component.root_element.borrow_mut();
        if let Some(b) = root.bindings.remove(prop) {
            root.bindings.insert(new_prop.name().clone(), b);
        };
        let mut analysis = root.property_analysis.borrow_mut();
        if let Some(a) = analysis.remove(prop) {
            analysis.insert(new_prop.name().clone(), a);
        };
        drop(analysis);
        root.bindings.insert(
            prop.into(),
            RefCell::new(Expression::PropertyReference(new_prop.clone()).into()),
        );
    }

    let old_prop = NamedReference::new(&component.root_element, SmolStr::new_static(prop));
    crate::object_tree::visit_all_named_references(component, &mut |nr| {
        if nr == &old_prop {
            *nr = new_prop.clone()
        }
    });
}
